package com.controller;

import com.util.GameUtil;
import com.view.GameFrame;

import javax.swing.*;
import java.awt.*;

/**
 * @author: Combo
 * @time: 2020/12/31 17:26
 * @description: 游戏进入加载动画
 */
public class WindowFrame extends JFrame implements Runnable {
    JLabel background; // 背景图
    JProgressBar progressBar; // 进度条

    public void start(){
        WindowFrame frame = new WindowFrame();
        Thread thread = new Thread(frame);
        // 启动线程
        thread.start();
        dispose(); // 关闭当前窗口
    }

    public WindowFrame() throws HeadlessException {
        background = new JLabel(GameUtil.getIcon("images/progress_back.jpg"));
        this.add(BorderLayout.NORTH, background);

        progressBar = new JProgressBar();
        progressBar.setStringPainted(true); // 字符串显示
        progressBar.setBackground(Color.orange);
        this.add(BorderLayout.SOUTH, progressBar);

        this.setSize(568, 340); // 加载界面窗口大小
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setUndecorated(true); // 不启用框架装饰 -- API 解释
        this.setIconImage(GameUtil.getImage("images/logo.png"));
        this.setVisible(true);

    }

    @Override
    public void run() {
        int[] val = {0,1,3,10,23,32,40,47,55,66,76,86,89,95,99,99,99,100};

        for (int i : val){
            progressBar.setValue(i);
            try {
                Thread.sleep(200); // 每次睡眠 200ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (i == 100){
                new GameFrame();
                dispose();
            }
        }
    }
}
